# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

aws = data_bag_item("aws", "knife-user")

if platform?(%w{debian ubuntu})
  bash "Import S3tools signing key" do
    code <<-EOH
    wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add -
    EOH
  end
  
  remote_file "/etc/apt/sources.list.d/s3tools.list" do
    source "http://s3tools.org/repo/deb-all/stable/s3tools.list"
    mode 0644
    action :create_if_missing
  end
else
  remote_file "/etc/yum.repos.d/s3tools.repo" do
    source "http://s3tools.org/repo/RHEL_6/s3tools.repo"
    mode 0644
    action :create_if_missing
  end
end

package "s3cmd" do
  action :install
  not_if "test -f /usr/bin/s3cmd"
end

unless File.exists?("/mnt/backup")
  directory "/mnt/backup" do
    owner "root"
    group "root"
    mode "0755"
    recursive true
  end
end

template "/mnt/backup/backup.sh" do
  source "backup.sh.erb"
  mode "0755"
  owner "root"
  group "root"
end

template "/mnt/backup/restore.sh" do
  source "restore.sh.erb"
  mode "0755"
  owner "root"
  group "root"
end

template "/mnt/backup/.s3cfg" do
  source "s3cfg.erb"
  mode "0600"
  owner "root"
  group "root"
  variables(
    :access_key => aws['aws_access_key_id'],
    :secret_key => aws['aws_secret_access_key']
  )
end

cron "lumis-backup" do
  hour "01"
  minute "00"
  command "/mnt/backup/backup.sh"
end
